package cn.com.backstage.controller;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.com.api.entity.ApiT_feedback;

import cn.com.api.service.ApiT_feedbackService;
import cn.com.api.service.StartOnLoadService;

import cn.com.util.*;
import cn.com.util.*;

@Controller
@RequestMapping(Utils.SYS_BACKSTAGE + "/")
public class T_feedbackController {

	private static Logger logger = Logger.getLogger(T_feedbackController.class);

	@Autowired
	private ApiT_feedbackService apiT_feedbackService;

	/**
	 * 进入查询界面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_feedback-list")
	public ModelAndView t_feedback_list(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_feedback_list.jsp");

		List<ApiT_feedback> t_feedbacks = apiT_feedbackService
				.findT_feedbacks();
		mv.addObject("t_feedbacks", t_feedbacks);

		return mv;
	}

	/**
	 * 进入新增或者编辑页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_feedback-add-pre")
	public ModelAndView t_feedback_add_pre(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ModelAndView mv = new ModelAndView(
				"forward:/backstage/t_feedback_add.jsp");
		//
		if (request.getParameter("id") != null) {
			String id = request.getParameter("id");
			ApiT_feedback t_feedback = apiT_feedbackService
					.findT_feedbackById(Integer.valueOf(id));
			mv.addObject("t_feedback", t_feedback);
		}
		return mv;
	}

	/**
	 * 新增或者编辑
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_feedback-add")
	public void t_feedback_add(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		String customer_id = request.getParameter("customer_id");
		String message = request.getParameter("message");
		String published = request.getParameter("published");
		String answer = request.getParameter("answer");
		// String create_date = request.getParameter("create_date");
		// String answer_date = request.getParameter("answer_date");
		// String answer_id = request.getParameter("answer_id");

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");
		// add
		if (id == null || (id.trim()).equals("")) {
			ApiT_feedback t_feedback = new ApiT_feedback();
			t_feedback.setCustomer_id(customer_id);
			t_feedback.setMessage(message);
			// t_feedback.setPublished(published);
			t_feedback.setAnswer(answer);
			t_feedback.setCreate_date(new Date());
			// t_feedback.setAnswer_date(answer_date);
			// t_feedback.setAnswer_id(answer_id);

			apiT_feedbackService.insertT_feedback(t_feedback);
		}
		// edit
		else {
			ApiT_feedback t_feedback = apiT_feedbackService
					.findT_feedbackById(Integer.valueOf(id));
			t_feedback.setCustomer_id(customer_id);
			t_feedback.setMessage(message);
			// t_feedback.setPublished(published);
			t_feedback.setAnswer(answer);
			// t_feedback.setCreate_date(create_date);
			// t_feedback.setAnswer_date(answer_date);
			// t_feedback.setAnswer_id(answer_id);

			apiT_feedbackService.updateT_feedback(t_feedback);
		}
		//
		map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
		map.put(Utils.MESSAGE, "添加成功！");
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

	/**
	 * 删除
	 * 
	 * @return
	 */
	@RequestMapping(value = "t_feedback-delete")
	public void t_feedback_delete(HttpServletRequest request,
			HttpServletResponse response) throws Exception {

		Map<String, Object> map = new HashMap<String, Object>();
		String id = request.getParameter("id");

		// delete
		int ret_i = apiT_feedbackService.deleteT_feedback(Integer.valueOf(id));

		//
		if (ret_i > 0) {
			map.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			map.put(Utils.MESSAGE, "删除成功！");
		}
		response.setCharacterEncoding("UTF-8");
		PrintWriter out = response.getWriter();
		out.print(JsonUtil.object2json(map));
		out.close();
	}

}
